// JavaScript Document
var xmlhttp;
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}
function viewCategory(id)
{
	window.location.href="ManageCategory.php?f=" + id;
}
function XuLyXoa(id, kind)
{
	var message = "";
	var url = "";
	if(kind == 1)
	{
		message = "Bạn muốn xóa lĩnh vực có mã ";
		url = "xl_ManageField.php";
	}
	if(kind == 2)
	{
		message = "Bạn muốn xóa danh mục có mã ";
		url = "xl_ManageCategory.php";
	}
	if(kind == 3)
	{
		message = "Bạn muốn xóa loại ebook có mã ";
		url = "xl_ManageEbookType.php";
	}
		
	if (confirm(message + id +"?"))
	{
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null)
		{
			alert ("Your browser does not support XML HTTP Request");
			return;
		}
		xmlhttp.onreadystatechange = stateChanged;
		
		url += "?deletedId=" + id;
		url += "&sid=" + Math.random();
		alert(url);
		xmlhttp.open("GET", url, true);
		xmlhttp.send(null);
	}
	else
		return;
}

function stateChanged()
{
	if (xmlhttp.readyState==4 && xmlhttp.status == 200)
	{
		document.getElementById("divResult").innerHTML=xmlhttp.responseText;
	}
}


function btnCapnhat_Click(node, kind)
{	
	var row = node.parentNode.parentNode;
	var table = row.parentNode;
	
	var updateRows = document.getElementsByClassName("updateRow");
	for(var i=0; i<updateRows.length; i++)
		table.deleteRow(updateRows[i].rowIndex);
		
	var id = table.rows[row.rowIndex - 1].cells[0].innerHTML;
	var oldName = table.rows[row.rowIndex].cells[1].innerHTML;	
	
	var newRow = table.insertRow(row.rowIndex + 1);
	newRow.className = "updateRow";
	newRow.innerHTML = "<td>&nbsp</td><td><input type='text' size='30' value='" + oldName + "'/></td>";
	if(kind == 2)
	{
		var oldField = table.rows[row.rowIndex].cells[2].innerHTML;
		var cbx = document.getElementById('cbbField');
		var lstOption = cbx.options;
		var selectTag = "<select id='cbbUpdateField'>";
		for(var i = 0; i < lstOption.length; i++)
		{
			if(oldField == lstOption[i].text)
				selectTag += "<option selected value='" + lstOption[i].value + "'>" + lstOption[i].text + "</option>";
			else
				selectTag += "<option value='" + lstOption[i].value + "'>" + lstOption[i].text + "</option>";
		}
		selectTag += "</select>";
		newRow.innerHTML += "<td>" + selectTag +  "</td>";
	}
	
	newRow.innerHTML += "<td><input type='button' onClick='XuLyCapNhat(this,"+ kind +")' value='Lưu'/><input type='button' onClick='btnHuy_Click(this)' value='Hủy'/></td>";
}

function btnHuy_Click(node)
{
	var row = node.parentNode.parentNode;
	var table = row.parentNode;
	table.deleteRow(row.rowIndex);
}

function XuLyCapNhat(node, kind)
{
	var row = node.parentNode.parentNode;
	var table = row.parentNode;
	var id = table.rows[row.rowIndex - 1].cells[0].innerHTML;
	var newName = row.cells[1].firstChild.value;
	
	var url = "";
	if(kind == 1)
	{
		if(newName == "")
		{
			alert("Vui lòng nhập tên mới cho lĩnh vực!");
			return;
		}	
		url = "xl_ManageField.php";
	}
	if(kind == 2)
	{
		if(newName == "")
		{
			alert("Vui lòng nhập tên mới cho danh mục!");
			return;
		}	
		url = "xl_ManageCategory.php";
	}
	if(kind == 3)
	{
		if(newName == "")
		{
			alert("Vui lòng nhập tên mới cho loại sách!");
			return;
		}	
		url = "xl_ManageEbookType.php";
	}
	
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	xmlhttp.onreadystatechange = stateChanged;
	url += "?updatedId=" + id;
	url += "&name=" + encodeURIComponent(newName);
	if(kind == 2)
	{
		var cbb = document.getElementById('cbbUpdateField');
		var f = cbb.options[cbb.selectedIndex].value;
		url += "&fld=" + encodeURIComponent(f);
	}
	
	url += "&sid=" + Math.random();
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function XuLyThem(kind)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	
	var url = "";
	if(kind == 1)
		url = "xl_ManageField.php";
	if(kind == 2)
		url = "xl_ManageCategory.php";
	if(kind == 3)
		url = "xl_ManageEbookType.php";
		
	xmlhttp.onreadystatechange = stateChanged;
	var name = document.getElementById("txtName").value;
	url += "?addedName=" + encodeURIComponent(name);
	if(kind == 2)
	{
		var cbx = document.getElementById('cbbField');
		var field = cbx.options[cbx.selectedIndex].value;
		url += "&f=" + field;
	}
	url += "&sid=" + Math.random();
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function showCategory(node)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	
	var url = "xl_ManageCategory.php";		
	xmlhttp.onreadystatechange = stateChanged;
	var field = node.options[node.selectedIndex].value;
	url += "?f=" + encodeURIComponent(field);
	url += "&sid=" + Math.random();
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}
